﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:FTP_New
    /// </summary>
    public partial class FTP_New
    {
        public FTP_New()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FParentID, string FLogID, string FEntityNo, decimal FID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_FTP_New");
            strSql.Append(" where FParentID=@FParentID and FLogID=@FLogID and FEntityNo=@FEntityNo and FID=@FID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FParentID", SqlDbType.Decimal,13),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FID", SqlDbType.Decimal,13)			};
            parameters[0].Value = FParentID;
            parameters[1].Value = FLogID;
            parameters[2].Value = FEntityNo;
            parameters[3].Value = FID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(ElingOA6_Admin_DSJCJ.Model.FTP_New model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_FTP_New(");
            strSql.Append("FParentID,FName,FSuffixName,FType,FSize,FDate,FLogID,FEntityNo,FIsUpload,FIsDownload)");
            strSql.Append(" values (");
            strSql.Append("@FParentID,@FName,@FSuffixName,@FType,@FSize,@FDate,@FLogID,@FEntityNo,@FIsUpload,@FIsDownload)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FParentID", SqlDbType.Decimal,13),
					new SqlParameter("@FName", SqlDbType.NVarChar,255),
					new SqlParameter("@FSuffixName", SqlDbType.NVarChar,20),
					new SqlParameter("@FType", SqlDbType.VarChar,1),
					new SqlParameter("@FSize", SqlDbType.Decimal,13),
					new SqlParameter("@FDate", SqlDbType.DateTime),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FIsUpload", SqlDbType.NVarChar,1),
					new SqlParameter("@FIsDownload", SqlDbType.NVarChar,1)};
            parameters[0].Value = model.FParentID;
            parameters[1].Value = model.FName;
            parameters[2].Value = model.FSuffixName;
            parameters[3].Value = model.FType;
            parameters[4].Value = model.FSize;
            parameters[5].Value = model.FDate;
            parameters[6].Value = model.FLogID;
            parameters[7].Value = model.FEntityNo;
            parameters[8].Value = model.FIsUpload;
            parameters[9].Value = model.FIsDownload;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.FTP_New model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_FTP_New set ");
            strSql.Append("FName=@FName,");
            strSql.Append("FSuffixName=@FSuffixName,");
            strSql.Append("FType=@FType,");
            strSql.Append("FSize=@FSize,");
            strSql.Append("FDate=@FDate,");
            strSql.Append("FIsUpload=@FIsUpload,");
            strSql.Append("FIsDownload=@FIsDownload");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FName", SqlDbType.NVarChar,255),
					new SqlParameter("@FSuffixName", SqlDbType.NVarChar,20),
					new SqlParameter("@FType", SqlDbType.VarChar,1),
					new SqlParameter("@FSize", SqlDbType.Decimal,13),
					new SqlParameter("@FDate", SqlDbType.DateTime),
					new SqlParameter("@FIsUpload", SqlDbType.NVarChar,1),
					new SqlParameter("@FIsDownload", SqlDbType.NVarChar,1),
					new SqlParameter("@FID", SqlDbType.Decimal,13),
					new SqlParameter("@FParentID", SqlDbType.Decimal,13),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FName;
            parameters[1].Value = model.FSuffixName;
            parameters[2].Value = model.FType;
            parameters[3].Value = model.FSize;
            parameters[4].Value = model.FDate;
            parameters[5].Value = model.FIsUpload;
            parameters[6].Value = model.FIsDownload;
            parameters[7].Value = model.FID;
            parameters[8].Value = model.FParentID;
            parameters[9].Value = model.FLogID;
            parameters[10].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_FTP_New ");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = FID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FParentID, string FLogID, string FEntityNo, decimal FID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_FTP_New ");
            strSql.Append(" where FParentID=@FParentID and FLogID=@FLogID and FEntityNo=@FEntityNo and FID=@FID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FParentID", SqlDbType.Decimal,13),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FID", SqlDbType.Decimal,13)			};
            parameters[0].Value = FParentID;
            parameters[1].Value = FLogID;
            parameters[2].Value = FEntityNo;
            parameters[3].Value = FID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_FTP_New ");
            strSql.Append(" where FID in (" + FIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }









        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FID,FParentID,FName,FSuffixName,FType,FSize,FDate,FLogID,FEntityNo,FIsUpload,FIsDownload ");
            strSql.Append(" FROM t_FTP_New ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_FTP_New ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FID desc");
            }
            strSql.Append(")AS Row, T.*  from t_FTP_New T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_FTP_New";
            parameters[1].Value = "FID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  已经使用的
        /// <summary>
        /// 获得数据列表
        /// <creator>sunlj</creator>
        /// <date>2014-09-22</date>
        /// </summary>
        public DataSet GetListByEntityNo(string FEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FID,FParentID,FName,FSuffixName,FType,FSize,FDate,FLogID,FEntityNo,FIsUpload,FIsDownload,FIsArchive ");
            strSql.Append(" FROM t_FTP_New ");
            strSql.Append(" WHERE FEntityNo=@FEntityNo and FType='D'");
            SqlParameter[] paramters = { 
                        new SqlParameter("@FEntityNo",SqlDbType.NVarChar,12)
                                       };
            paramters[0].Value = FEntityNo;
            return DbHelperSQL.Query(strSql.ToString(), paramters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.FTP_New DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.FTP_New model = new ElingOA6_Admin_DSJCJ.Model.FTP_New();
            if (row != null)
            {
                if (row["FID"] != null && row["FID"].ToString() != "")
                {
                    model.FID = decimal.Parse(row["FID"].ToString());
                }
                if (row["FParentID"] != null && row["FParentID"].ToString() != "")
                {
                    model.FParentID = decimal.Parse(row["FParentID"].ToString());
                }
                if (row["FName"] != null)
                {
                    model.FName = row["FName"].ToString();
                }
                if (row["FSuffixName"] != null)
                {
                    model.FSuffixName = row["FSuffixName"].ToString();
                }
                if (row["FType"] != null)
                {
                    model.FType = row["FType"].ToString();
                }
                if (row["FSize"] != null && row["FSize"].ToString() != "")
                {
                    model.FSize = decimal.Parse(row["FSize"].ToString());
                }
                if (row["FDate"] != null && row["FDate"].ToString() != "")
                {
                    model.FDate = DateTime.Parse(row["FDate"].ToString());
                }
                if (row["FLogID"] != null)
                {
                    model.FLogID = row["FLogID"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FIsUpload"] != null)
                {
                    model.FIsUpload = row["FIsUpload"].ToString();
                }
                if (row["FIsDownload"] != null)
                {
                    model.FIsDownload = row["FIsDownload"].ToString();
                }
                if (row.Table.Columns.Contains("FUserName"))
                {
                    if (row["FUserName"] != null)
                    {
                        model.FUserName = row["FUserName"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FIsArchive"))
                {
                    model.FIsArchive = row["FIsArchive"].ToString();
                }
                if (row.Table.Columns.Contains("FDeptNo"))
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row.Table.Columns.Contains("FDeptName"))
                {
                    model.FDeptName = row["FDeptName"].ToString();
                }
            }
            return model;
        }

        public DataTable GetVModelList(string strwhere)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
SELECT   FChargeLeader ,
        FDate ,
        FDeptName ,
        v_FTP.FDeptNo ,
        t_HRDept.FDeptNo ,
        FDeptProp ,
        FDeptShorName ,
        v_FTP.FEntityNo ,
        t_HRDept.FEntityNo ,
        FID ,
        FIsAgency ,
        FIsCheckSystem ,
        FIsDownload ,
        FIsEval ,
        FIsReportPlanSummary ,
        FIsUpload ,
        FLogID ,
        FName ,
        FParentDeptNo ,
        FParentID ,
        FSeq ,
        FSize ,
        FSuffixName ,
        FType ,
        FUnionScoreType ,
        FUserName
         FROM dbo.v_FTP INNER JOIN dbo.t_HRDept ON t_HRDept.FDeptNo = v_FTP.FDeptNo AND t_HRDept.FEntityNo = v_FTP.FEntityNo");
            if (!string.IsNullOrEmpty(strwhere))
            {
                sb.Append(" where " + strwhere);
            }
            return DbHelperSQL.Query(sb.ToString()).Tables[0];
        }

        public DataTable GetAllFileCteByDirOrFileFid(string strWhere)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"

WITH    cte
          AS ( SELECT   FID ,
                        FParentID ,
                        FName ,
                        FSuffixName ,
                        FType ,
                        FSize ,
                        FDate ,
                        FLogID ,
                        FEntityNo ,
                        FIsUpload ,
                        FIsDownload,
                        FIsArchive,
                         FDeptNo,
                         FDeptName,
                        0 idLevel 
               FROM     dbo.v_FTP 
             WHERE " + strWhere + @"
               UNION ALL
               SELECT   a.FID ,
                        a.FParentID ,
                        a.FName ,
                        a.FSuffixName ,
                        a.FType ,
                        a.FSize ,
                        a.FDate ,
                        a.FLogID ,
                        a.FEntityNo ,
                        a.FIsUpload ,
                        a.FIsDownload,
                        a.FIsArchive,
                        a.FDeptNo,
                        a.FDeptName,
                        b.idLevel+1
               FROM     dbo.v_FTP a ,
                        cte b
               WHERE    b.FID = a.FParentID 
             )
             
         SELECT *     FROM    cte     WHERE FType='F'");

            return DbHelperSQL.Query(sb.ToString()).Tables[0];
            }
        /// <summary>
        /// 获取子栏目数量
        /// <creator>sunlj</creator>
        /// <date>2014-09-22</date>
        /// </summary>
        /// <param name="id"></param>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public int GetChildCount(decimal id, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(*) FROM dbo.t_FTP_New WHERE FEntityNo=@FEntityNo ");
            strSql.Append(" AND FID=@FID and FType='D' ");
            SqlParameter[] parameters = { 
                            new SqlParameter("@FEntityNo",SqlDbType.NVarChar,12),
                            new SqlParameter("@FID",SqlDbType.Decimal)
                                        };
            parameters[0].Value = entityNo;
            parameters[1].Value = id;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return int.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.FTP_New GetModel(decimal FID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FID,FParentID,FName,FSuffixName,FType,FSize,FDate,FLogID,FEntityNo,FIsUpload,FIsDownload,FIsArchive from t_FTP_New ");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = FID;

            ElingOA6_Admin_DSJCJ.Model.FTP_New model = new ElingOA6_Admin_DSJCJ.Model.FTP_New();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 增加目录
        /// <creator>sunlj</creator>
        /// <date>2014-09-22</date>
        /// </summary>
        /// <param name="FTP_NewModel"></param>
        /// <returns></returns>
        public decimal AddFTP_New(Model.FTP_New FTP_NewModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_FTP_New(");
            strSql.Append("FParentID,FName,FSuffixName,FType,FSize,FDate,FLogID,FEntityNo,FIsUpload,FIsDownload,FIsArchive)");
            strSql.Append(" values (");
            strSql.Append("@FParentID,@FName,@FSuffixName,@FType,@FSize,GETDATE(),@FLogID,@FEntityNo,@FIsUpload,@FIsDownload,@FIsArchive)");
            strSql.Append(";SELECT @returnId=IDENT_CURRENT('t_FTP_New')");
            SqlParameter[] parameters = {
					new SqlParameter("@FParentID", SqlDbType.Decimal,13),
					new SqlParameter("@FName", SqlDbType.NVarChar,255),
					new SqlParameter("@FSuffixName", SqlDbType.NVarChar,20),
					new SqlParameter("@FType", SqlDbType.VarChar,1),
					new SqlParameter("@FSize", SqlDbType.Decimal,13),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FIsUpload", SqlDbType.NVarChar,1),
					new SqlParameter("@FIsDownload", SqlDbType.NVarChar,1),
                    new SqlParameter("@returnId",SqlDbType.Decimal),
                 new SqlParameter("@FIsArchive", SqlDbType.VarChar,1)   
                                        };
            parameters[0].Value = FTP_NewModel.FParentID;
            parameters[1].Value = FTP_NewModel.FName;
            parameters[2].Value = FTP_NewModel.FSuffixName;
            parameters[3].Value = FTP_NewModel.FType;
            parameters[4].Value = FTP_NewModel.FSize;
            parameters[5].Value = FTP_NewModel.FLogID;
            parameters[6].Value = FTP_NewModel.FEntityNo;
            parameters[7].Value = FTP_NewModel.FIsUpload;
            parameters[8].Value = FTP_NewModel.FIsDownload;
            parameters[9].Direction = ParameterDirection.Output;
            parameters[10].Value = FTP_NewModel.FIsArchive;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return decimal.Parse(parameters[9].Value.ToString());
        }

        /// <summary>
        /// 修改目录
        /// <creator>sunlj</creator>
        /// <date>2014-09-22</date>
        /// </summary>
        /// <param name="FTP_NewModel"></param>
        /// <returns></returns>
        public void UpdateFTP_New(Model.FTP_New FTP_NewModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_FTP_New set ");
            strSql.Append("FName=@FName,");
            strSql.Append("FSuffixName=@FSuffixName,");
            strSql.Append("FType=@FType,");
            strSql.Append("FSize=@FSize,");
            strSql.Append("FDate=@FDate,");
            strSql.Append("FIsUpload=@FIsUpload,");
            strSql.Append("FIsDownload=@FIsDownload");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FName", SqlDbType.NVarChar,255),
					new SqlParameter("@FSuffixName", SqlDbType.NVarChar,20),
					new SqlParameter("@FType", SqlDbType.VarChar,1),
					new SqlParameter("@FSize", SqlDbType.Decimal,13),
					new SqlParameter("@FDate", SqlDbType.DateTime),
					new SqlParameter("@FIsUpload", SqlDbType.NVarChar,1),
					new SqlParameter("@FIsDownload", SqlDbType.NVarChar,1),
					new SqlParameter("@FID", SqlDbType.Decimal,13),
					new SqlParameter("@FParentID", SqlDbType.Decimal,13),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = FTP_NewModel.FName;
            parameters[1].Value = FTP_NewModel.FSuffixName;
            parameters[2].Value = FTP_NewModel.FType;
            parameters[3].Value = FTP_NewModel.FSize;
            parameters[4].Value = FTP_NewModel.FDate;
            parameters[5].Value = FTP_NewModel.FIsUpload;
            parameters[6].Value = FTP_NewModel.FIsDownload;
            parameters[7].Value = FTP_NewModel.FID;
            parameters[8].Value = FTP_NewModel.FParentID;
            parameters[9].Value = FTP_NewModel.FLogID;
            parameters[10].Value = FTP_NewModel.FEntityNo;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 归档
        /// </summary>
        /// <param name="id"></param>
        public void ChangeArchiveY(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"WITH    cte
          AS ( SELECT   FID ,
                        FParentID ,
                        FName ,
                        FSuffixName ,
                        FType ,
                        FSize ,
                        FDate ,
                        FLogID ,
                        FEntityNo ,
                        FIsUpload ,
                        FIsDownload,
                        FIsArchive,
                        0 idLevel 
               FROM     dbo.t_FTP_New
               WHERE    FID = @FID
               UNION ALL
               SELECT   a.FID ,
                        a.FParentID ,
                        a.FName ,
                        a.FSuffixName ,
                        a.FType ,
                        a.FSize ,
                        a.FDate ,
                        a.FLogID ,
                        a.FEntityNo ,
                        a.FIsUpload ,
                        a.FIsDownload,
                        a.FIsArchive,
                        b.idLevel+1
               FROM     dbo.t_FTP_New a ,
                        cte b
               WHERE    b.FID = a.FParentID 
             )
             
       UPDATE dbo.t_FTP_New SET FIsArchive='Y' WHERE FID in  (    SELECT  FID
    FROM    cte)");
            
            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 取消归档
        /// </summary>
        /// <param name="id"></param>
        public void ChangeArchiveN(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"WITH    cte
          AS ( SELECT   FID ,
                        FParentID ,
                        FName ,
                        FSuffixName ,
                        FType ,
                        FSize ,
                        FDate ,
                        FLogID ,
                        FEntityNo ,
                        FIsUpload ,
                        FIsDownload,
                        FIsArchive,
                        0 idLevel 
               FROM     dbo.t_FTP_New
               WHERE    FID = @FID
               UNION ALL
               SELECT   a.FID ,
                        a.FParentID ,
                        a.FName ,
                        a.FSuffixName ,
                        a.FType ,
                        a.FSize ,
                        a.FDate ,
                        a.FLogID ,
                        a.FEntityNo ,
                        a.FIsUpload ,
                        a.FIsDownload,
                        a.FIsArchive,
                        b.idLevel+1
               FROM     dbo.t_FTP_New a ,
                        cte b
               WHERE    b.FID = a.FParentID 
             )
             
       UPDATE dbo.t_FTP_New SET FIsArchive='N' WHERE FID in  (    SELECT  FID
    FROM    cte)");

            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 删除目录
        /// <creator>sunlj</creator>
        /// <date>2014-09-22</date>
        /// </summary>
        /// <param name="id"></param>
        public void deleteFTP_New(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_FTP_New ");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 判断该来栏目是否存在文件
        /// <creator>sunlj</creator>
        /// <date>2014-09-24</date>
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ExistFile(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(*) FROM dbo.t_FTP_New WHERE FType='F' AND FParentID=@FParentID");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FParentID",SqlDbType.Decimal)
                                        };
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 判断该来栏目是否存在下级栏目
        /// <creator>sunlj</creator>
        /// <date>2014-09-24</date>
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ExistChildFTP_New(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(*) FROM dbo.t_FTP_New WHERE FType='D' AND FParentID=@FParentID");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FParentID",SqlDbType.Decimal)
                                        };
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取当前目录的所有父级目录
        /// <creator>sunlj</creator>
        /// <date>2014-09-25</date>
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataSet GetListPre(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT  FID ,
        FParentID ,
        FName ,
        FSuffixName ,
        FType ,
        FSize ,
        FDate ,
        FLogID ,
        FEntityNo ,
        FIsUpload ,
        FIsDownload,
        idLevel FROM fn_GetEnterpriseFTP_Pre(@FID)
        ORDER BY idLevel");
            SqlParameter[] parameters = { 
                            new SqlParameter("@FID",SqlDbType.Decimal)
                                        };
            parameters[0].Value = id;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 批量增加FTP_New
        /// <creator>sunlj</creator>
        /// <date>2014-09-26</date>
        /// </summary>
        /// <param name="FTP_NewList"></param>
        public void AddFTP_NewList(List<Model.FTP_New> FTP_NewList)
        {
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[] { };
            CommandInfo commandInfo = new CommandInfo();
            List<CommandInfo> commandInfoList = new List<CommandInfo>();


            foreach (Model.FTP_New FTP_NewModel in FTP_NewList)
            {
                strSql = new StringBuilder();
                strSql.Append("insert into t_FTP_New(");
                strSql.Append("FParentID,FName,FSuffixName,FType,FSize,FDate,FLogID,FEntityNo,FIsUpload,FIsDownload)");
                strSql.Append(" values (");
                strSql.Append("@FParentID,@FName,@FSuffixName,@FType,@FSize,@FDate,@FLogID,@FEntityNo,@FIsUpload,@FIsDownload)");
                parameters = new SqlParameter[] {
					new SqlParameter("@FParentID", SqlDbType.Decimal,13),
					new SqlParameter("@FName", SqlDbType.NVarChar,255),
					new SqlParameter("@FSuffixName", SqlDbType.NVarChar,20),
					new SqlParameter("@FType", SqlDbType.VarChar,1),
					new SqlParameter("@FSize", SqlDbType.Decimal,13),
					new SqlParameter("@FDate", SqlDbType.DateTime),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FIsUpload", SqlDbType.NVarChar,1),
					new SqlParameter("@FIsDownload", SqlDbType.NVarChar,1)
                                        };
                parameters[0].Value = FTP_NewModel.FParentID;
                parameters[1].Value = FTP_NewModel.FName;
                parameters[2].Value = FTP_NewModel.FSuffixName;
                parameters[3].Value = FTP_NewModel.FType;
                parameters[4].Value = FTP_NewModel.FSize;
                parameters[5].Value = FTP_NewModel.FDate;
                parameters[6].Value = FTP_NewModel.FLogID;
                parameters[7].Value = FTP_NewModel.FEntityNo;
                parameters[8].Value = FTP_NewModel.FIsUpload;
                parameters[9].Value = FTP_NewModel.FIsDownload;

                commandInfo = new CommandInfo(strSql.ToString(), parameters);
                commandInfoList.Add(commandInfo);
            }

            if (commandInfoList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithIndentity(commandInfoList);
            }

        }


        /// <summary>
        /// 获得数据列表
        /// <creator>sunlj</creator>
        /// <date>2014-09-26</date>
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FID,FParentID,FName,FSuffixName,FType,FSize,FDate,FLogID,FEntityNo,FIsUpload,FIsDownload,FIsArchive ");
            strSql.Append(" FROM t_FTP_New ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 移动文件位置
        /// <creator>sunlj</creator>
        /// <date>2014-09-30</date>
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="newParentID"></param>
        public void moveFTP_NewFile(decimal FID, decimal newParentID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE t_FTP_New SET FParentID=@FParentID WHERE FID=@FID");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FParentID",SqlDbType.Decimal),
                        new SqlParameter("@FID",SqlDbType.Decimal)
                                        };
            parameters[0].Value = newParentID;
            parameters[1].Value = FID;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 重命名
        /// <creator>sunlj</creator>
        /// <date>2014-09-30</date>
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="FName"></param>
        public void renameFTP_NewFile(decimal FID, string FName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE dbo.t_FTP_New SET FName=@FName WHERE FID=@FID");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FName",SqlDbType.NVarChar,255),
                        new SqlParameter("@FID",SqlDbType.Decimal)
                                        };
            parameters[0].Value = FName;
            parameters[1].Value = FID;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 判断新的文件夹中是否有同名的文件
        /// <creator>sunlj</creator>
        /// <date>2014-10-10</date>
        /// </summary>
        /// <param name="newFParentID"></param>
        /// <param name="strOldFID"></param>
        /// <returns></returns>
        public bool ExistSameName(decimal newFParentID, string strOldFID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(*) FROM dbo.t_FTP_New WHERE FParentID=@FParentID AND FName IN (SELECT FName FROM dbo.t_FTP_New WHERE FID IN (" + strOldFID + "))");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FParentID",SqlDbType.Decimal)
                                        };
            parameters[0].Value = newFParentID;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 判断新的文件夹中是否有同名的文件
        /// <creator>sunlj</creator>
        /// <date>2014-10-10</date>
        /// </summary>
        /// <param name="newFParentID"></param>
        /// <param name="strOldFID"></param>
        /// <returns></returns>
        public bool ExistSameNameWithNewName(decimal newFParentID, string newName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(*) FROM dbo.t_FTP_New WHERE FParentID=@FParentID AND FName=@FName");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FParentID",SqlDbType.Decimal),
                        new SqlParameter("@FName",SqlDbType.NVarChar,255),
                                        };
            parameters[0].Value = newFParentID;
            parameters[1].Value = newName;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 判断新的文件夹中是否有同名的文件
        /// <creator>sunlj</creator>
        /// <date>2014-10-10</date>
        /// </summary>
        /// <param name="newFParentID"></param>
        /// <param name="strOldFID"></param>
        /// <returns></returns>
        public bool ExistSameNameWithNewNameWhenNull(string newName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(*) FROM dbo.t_FTP_New WHERE FParentID is null AND FName=@FName");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FName",SqlDbType.NVarChar,255),
                                        };
            parameters[0].Value = newName;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        #endregion  已经使用的
    }
}


